﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using TFXK.BLL;
using log4net;
using System.Collections.Generic;
using TFXK.Model;


using System.IO;
using System.Net;
using System.Web.Caching;


[PartialCaching(7200)]
public partial class MyControls_WeatherControl : System.Web.UI.UserControl
{

    private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private static readonly CategoryBLL bllCategory = new CategoryBLL();
    int rowCount = 0;
    public int k = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindCityWeater();
        }
    }

    #region 绑定天气预报city
    public void bindCityWeater()
    {
       DataSet ds = bllCategory.GetNextNodeByCode("gzdcs");
       if (ds != null && ds.Tables[0].Rows.Count > 0) {
           while (ds.Tables[0].Rows.Count > 10) {
               ds.Tables[0].Rows.RemoveAt(10);
           }
       }
       rptCityWeather.DataSource = ds;
        rptCityWeather.DataBind();
    }

    #endregion

    #region

    ///    <summary > 
    ///    传入URL返回网页的html代码 
    ///    </summary > 
    ///    <param    name="Url" >URL </param > 
    ///    <returns > </returns > 
    private string getUrltoHtml(string Url)
    {
        string errorMsg = "";
        try
        {
            System.Net.WebRequest wReq = System.Net.WebRequest.Create("Http://" + Url);
            System.Net.WebResponse wResp = wReq.GetResponse();
            System.IO.Stream respStream = wResp.GetResponseStream();
            System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("GB2312"));
            return reader.ReadToEnd();
        }
        catch (System.Exception ex)
        {
            errorMsg = ex.Message;
        }
        return "";
    }

    public string GetWeather(object City)
    {
        //成都&nbsp;&nbsp;&nbsp;<img src="images/index_87.jpg" width="20" height="22" />&nbsp;&nbsp;&nbsp;32~19&nbsp;&nbsp;&nbsp;微风&nbsp;&nbsp;&nbsp;七天预报</li>
        //string weather = Cache.Get("tianqi") + "";
        //if (string.IsNullOrEmpty(weather))
        //{
        try
        {
            string Result = string.Empty;
            string Html = "";
            try
            {
                Html = getUrltoHtml("php.weather.sina.com.cn/search.php?city=" + City + "&dpc=1");

            }
            catch (Exception err)
            {
                Result = "访问地址出错";
            }
            int firstDayAndCityStartIndex = Html.IndexOf("<h1>", 0);
            int firstDayAndCityEndIndex = Html.IndexOf("</h1>", 0);
            string FirstDayAndCity = Html.Substring(firstDayAndCityStartIndex + 4, firstDayAndCityEndIndex - firstDayAndCityStartIndex - 4);


            firstDayAndCityEndIndex = Html.IndexOf("<div class=\"col\"> <span class=\"day_s\">", 0);

            //白天天气
            int firstTemperatureWeatherStartIndex = Html.IndexOf("<h2>", firstDayAndCityEndIndex);
            int firstTemperatureWeatherEndIndex = Html.IndexOf("</h2>", firstTemperatureWeatherStartIndex);
            string Weatherday = Html.Substring(firstTemperatureWeatherStartIndex + 4, firstTemperatureWeatherEndIndex - firstTemperatureWeatherStartIndex - 4);

            Result = " <li><table style='width:100%'><tr><td>" + City + "</td><td>" + Weatherday;


            //白天温度
            firstTemperatureWeatherStartIndex = Html.IndexOf("<div class=\"w-number\"> <span class=\"tpte\">", firstTemperatureWeatherEndIndex);
            firstTemperatureWeatherStartIndex = Html.IndexOf("<span class=\"tpte\">", firstTemperatureWeatherStartIndex);

            firstTemperatureWeatherEndIndex = Html.IndexOf("</span> </div>", firstTemperatureWeatherStartIndex) - 19;
            Weatherday = Html.Substring(firstTemperatureWeatherStartIndex + 19, firstTemperatureWeatherEndIndex - firstTemperatureWeatherStartIndex);
            Result += "</td><td>" + Weatherday;
            //夜间天气
            firstTemperatureWeatherStartIndex = Html.IndexOf("<span class=\"day_s\">夜间</span>", firstTemperatureWeatherEndIndex);
            firstTemperatureWeatherStartIndex = Html.IndexOf("<div class=\"w-icon\">", firstTemperatureWeatherStartIndex);
            firstTemperatureWeatherStartIndex = Html.IndexOf("<h2>", firstTemperatureWeatherStartIndex);

            firstTemperatureWeatherEndIndex = Html.IndexOf("</h2>", firstTemperatureWeatherStartIndex);
            Weatherday = Html.Substring(firstTemperatureWeatherStartIndex + 4, firstTemperatureWeatherEndIndex - firstTemperatureWeatherStartIndex - 4);
            // lblWeather.Text = lblWeather.Text + "/" + Weatherday;


            //夜间温度
            firstTemperatureWeatherStartIndex = Html.IndexOf("<span class=\"tpte\">", firstTemperatureWeatherEndIndex);
            firstTemperatureWeatherEndIndex = Html.IndexOf("</span> </div>", firstTemperatureWeatherStartIndex);
            Weatherday = Html.Substring(firstTemperatureWeatherStartIndex + 19, firstTemperatureWeatherEndIndex - firstTemperatureWeatherStartIndex - 19);
            //  lblTemperature.Text = lblTemperature.Text + "/" + Weatherday;
            Result += "~" + Weatherday;

            //体感温度
            firstTemperatureWeatherStartIndex = Html.IndexOf("<span class=\"tpte\">", firstTemperatureWeatherEndIndex);
            firstTemperatureWeatherEndIndex = Html.IndexOf("</span></li>", firstTemperatureWeatherStartIndex);
            Weatherday = Html.Substring(firstTemperatureWeatherStartIndex + 19, firstTemperatureWeatherEndIndex - firstTemperatureWeatherStartIndex - 19);
            //  lblSense.Text = Weatherday;





            //风向
            firstTemperatureWeatherStartIndex = Html.IndexOf("<li>", firstTemperatureWeatherEndIndex);
            firstTemperatureWeatherEndIndex = Html.IndexOf("</li>", firstTemperatureWeatherStartIndex) - 6;
            Weatherday = Html.Substring(firstTemperatureWeatherStartIndex + 7, firstTemperatureWeatherEndIndex - firstTemperatureWeatherStartIndex - 1);
            // lblwinddirection.Text = Weatherday;
            //Result +="&nbsp;&nbsp;" + Weatherday;

            string str = Result.Replace("℃", "") + "</td><td><td style='width:100px; text-align:left;'><a href='http://php.weather.sina.com.cn/search.php?city=" + City + "&dpc=1' target='_blank'>七天预报</a></td></tr></table></li>";
            //Cache.Insert("tianqi", str, null, DateTime.Now.AddHours(2), Cache.NoSlidingExpiration);
            return str;
        }
        catch
        {
            return "";
        }
        //}
        //else
        //{
        //    return weather;
        //}
    }
    #endregion

}
